﻿using DevExpress.XtraBars;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevUIUtils
{
    public static class XtraEditorsEx
    {
        /// <summary>
        /// ButtonEdit选择文件
        /// </summary>
        /// <param name="btnEdt">要扩展的ButtonEdit对象</param>
        /// <param name="filter">文件选择对话框选择文件过滤字符串。
        /// 格式："[自己定义名称1]|*.exe;*.db|[自定义名称2]|*.xls;*.doc"</param>
        public static void SelectFile(this ButtonEdit btnEdt, string filter)
        {
            btnEdt.ButtonClick += (sender, e) =>
            {
                OpenFileDialog dialog = new OpenFileDialog() { Filter = filter };
                DialogResult dgRet = dialog.ShowDialog(btnEdt);
                if (dgRet == DialogResult.OK)
                {
                    btnEdt.Text = dialog.FileName;
                }
            };
        }

        /// <summary>
        /// 选择文件夹
        /// </summary>
        public static void SelectDirectory(this ButtonEdit btnEdt, string defPath)
        {
            btnEdt.ButtonClick += (sender, e) =>
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                if (!string.IsNullOrWhiteSpace(defPath)) dialog.SelectedPath = defPath;
                var dRet = dialog.ShowDialog(btnEdt);
                if (dRet == DialogResult.OK)
                {
                    btnEdt.Text = dialog.SelectedPath;
                }
            };
        }

        /// <summary>
        /// 按钮导出GridView为Excel
        /// </summary>
        /// <param name="btn">按钮对象</param>
        /// <param name="gridView">要导出Excel的表格对象</param>
        public static void OutExcel(this SimpleButton btn, GridView gridView)
        {
            gridView.RowCountChanged += (sender, e) => btn.Enabled = gridView.RowCount > 0;
            btn.Click += (send, e) => gridView.ExportExcel();
        }

        /// <summary>
        /// 快捷菜单按钮导出Excel
        /// </summary>
        /// <param name="btn">快捷菜单按钮对象</param>
        /// <param name="gridView">要导出Excel的表格对象</param>
        public static void OutExcel(this BarBaseButtonItem btn, GridView gridView)
        {
            gridView.RowCountChanged += (sender, e) => btn.Enabled = gridView.RowCount > 0;
            btn.ItemClick += (send, e) => gridView.ExportExcel();
        }



        /// <summary>
        /// 设置TextEdit按回车后激活下一个TextEdit
        /// </summary>
        /// <param name="control"></param>
        /// <param name="nextControl"></param>
        public static void EnterNextControl(this Control control, Control nextControl)
        {
            control.KeyDown += (s, e) =>
            {
                if (e.KeyCode == Keys.Enter)
                {
                    nextControl.Focus();
                }
            };
        }



    }
}
